import request from '@/utils/request'

// 获取管理员列表
export function getAdminList(params: AdminListParamType) {
  return request<AdminListType>({
    url: '/setting/admin',
    method: 'get',
    params,
  })
}

// 添加管理员
export function insertAdmin(data: AdminFormType) {
  return request({
    url: '/setting/admin',
    method: 'post',
    data,
  })
}

// 更新管理员信息
export function updateAdmin(id: number, data: AdminFormType) {
  return request({
    url: `/setting/admin/${id}`,
    method: 'put',
    data,
  })
}

// 删除管理员
export function deleteAdmin(id: number) {
  return request({
    url: `/setting/admin/${id}`,
    method: 'delete',
  })
}

// 更新管理员账号状态
export function updateAdminStatus(id: number, status: number) {
  return request({
    url: `/setting/set_status/${id}/${status}`,
    method: 'put',
  })
}

// 获取管理员信息
export function getAdminInfo(id: number) {
  const adminInfo: any = {}

  return request({
    url: `/setting/admin/${id}/edit`,
    method: 'get',
  }).then(res => {
    res.rules.forEach((item: any) => {
      adminInfo[item.field] = item.value
    })

    return Promise.resolve(adminInfo as AdminFormType)

    // adminInfo as AdminInfoType
  })
}
